/**
 * Created by steven on 2016/4/22.
 */
$("#searchbutton").on('click',function(){
    var q="<div>sunny</div>"+"<p>"+$("#question").val()+"</p>";
    $("#dialog").append(q);
    var url=$("#machineApi").html();
    var key=$("#machineAppid").html();
    var machineSecret=$("#machineSecret").html();
    var info=$("#question").val();
    var userid=$("#userid").html();
    var aj = $.ajax( {
        url:url,// 跳转到 action
        data:{
        info:info,
        key:key,
        userid:userid
       },
        type:'post',
        cache:false,
        dataType:'json',
       success:function(data) {
           var code=data.code;
           var title="<div>小法：</div>";
           var answer="";
           switch (code){
               case 100000:
                   answer=title+ "<div>"+data.text+"</div>";
                   break;
               case 200000:
                   answer=title+ "<div>"+data.text+"</div>"+"<div>"+data.url+"</div>";
                   break;
               case 302000:
                   var article_list=data.list;
                   var articlediv='';
                   $.each(article_list,function(n,value) {
                       articlediv = articlediv + "<div>" + value.article + "</div>" + "<div>新闻来源：" + value.source + "</div>" +
                           "<div><a>" + value.detailurl + "</a></div>";
                   });
                   answer=title+ articlediv;
                   break;
               case 308000:
                   var article_list=data.list;
                   var articlediv='';
                   $.each(article_list,function(n,value) {
                       articlediv = articlediv + "<div>" + value.name + "</div>" + "<div>配料：" + value.info + "</div>" +
                           "<div><a>" + value.detailurl + "</a></div>";
                   });
                   answer=title+ articlediv;
                   break;
           }

            $("#dialog").append(answer);
            $("#dialog").scrollTop = $("#dialog").scrollHeight;

          },
        error : function() {
                  // view("异常！");
                  alert("异常！");
            }
     });
});